package chapter_06_prototype;

import java.util.ArrayList;

/**
 * Created by CharloYao on 2017/3/14.
 */
public class Person implements Cloneable {
    private String name;
    private int age;

    private Dog dog = new Dog();

    public Person() {
    }

    public Person(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    protected Object clone() {
        Person p = null;
        try {
            p = (Person) super.clone();

            p.dog = (Dog) p.dog.clone();        //注释掉该行则Person为潜copy
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return p;
    }

    public static void main(String[] args) throws CloneNotSupportedException {

        Dog dog1 = new Dog("dog1", 1);
        Person person = new Person(dog1);

        Person copyPerson = (Person) person.clone();        //复制了一个person对象，并返回其引用
        Dog copyDog = copyPerson.getDog();

        System.out.println("person == copyPerson："+ (person == copyPerson));                //false，不同的person，引用不
        /**
         * 潜copy时为true：表示两个dog为同一个对象的引用
         * 深copy时为false：表示两个不同对象的引用
         */
        System.out.println("dog1 == copyDog："+(dog1 == copyDog));

    }
}
